﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TMT.Data;
using TMT.Data.Base;
using TMT.Data.Common;

namespace TMT.Admin
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            btnLogin.Click += new EventHandler(btnLogin_Click);
            pnlError.Visible = false;
        }

        void btnLogin_Click(object sender, EventArgs e)
        {
            AccountMaster account = GetAccountFromView();
            OperationResult loginResult = AccountService.Instance.Login(account);
            if (loginResult.Result)
            {
                if (chkRemember.Checked)
                {
                    // khai báo biến cookie
                    HttpCookie cookieAccount = new HttpCookie("TMTAccount");
                    cookieAccount.Values["UserId"] = account.UserId;
                    cookieAccount.Values["Password"] = account.Password;
                    // Gán thời gian sống của Cookie là 30 ngày
                    cookieAccount.Expires = DateTime.Now.AddDays(30);

                    // Thêm Cookie
                    Response.Cookies.Add(cookieAccount);
                }
                else
                {
                    Session["TMTAccount"] = account;
                }
                Response.Redirect("Default.aspx");
            }
            else
            {
                lblerror.Text = loginResult.Message;
                pnlError.Visible = true;
            }
        }

        private AccountMaster GetAccountFromView()
        {
            AccountMaster account = new AccountMaster()
            {
                UserId = txtUserName.Text,
                Password = txtPassword.Text
            };
            return account;
        }
    }
}